package com.samtech.ftp.service.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;


import com.samtech.ftp.service.FtpException;
import com.samtech.ftp.service.FtpService;

public class CommonFtpServiceImpl implements FtpService {
    FTPClient client;
    String host;
    String userName;
    String password;
    
    public void closeServer() throws  FtpException {
        try {
            client.disconnect();
        } catch (IOException e) {
           FtpException ftpException = new FtpException();
           ftpException.initCause(e);
           throw ftpException;
        }

    }

    public void connectServer(String server, String user, String password, boolean passiveModeTransfer)
            throws  FtpException {
        try {
        client = new FTPClient();
        userName = user;
        this.password = password;
        FTPClientConfig cfg = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
        // cfg.setLenientFutureDates(lenientFutureDates)
        client.configure(cfg);
        int indexOf = server.indexOf("/");
        String s = server;
        String path = null;
        if (indexOf > 4) {
            s = server.substring(0, indexOf);
            host = s;
            path = server.substring(indexOf);
        }
        client.connect(s);
        client.setControlEncoding("GBK");
        if(passiveModeTransfer)
        client.setFileTransferMode(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
        client.login(user, password);
        client.cwd(path);
        } catch (IOException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        }

    }

    public void deleteFile(String fileName) throws  FtpException {
        try {
            client.deleteFile(fileName);
        } catch (IOException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        }

    }

    public void download(String fileName, String localFile) throws  FtpException {
        OutputStream local;
        try {
            local = new FileOutputStream(localFile);
            client.retrieveFile(fileName, local);
        } catch (FileNotFoundException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        } catch (IOException e) {
            
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        }

    }

    public void downloadAndDeleteFile(String fileName, String newFileName) throws  FtpException {
        download(fileName, newFileName);
        try {
            client.deleteFile(fileName);
        } catch (IOException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        }
    }

    public List<String> getFileList(String path) throws  FtpException {
        FTPFile[] listFiles;
        try {
            listFiles = client.listFiles(path);
            List files = new ArrayList();
            if (listFiles != null && listFiles.length > 0) {
                for (int i = 0; i < listFiles.length; i++) {
                    FTPFile ftpFile = listFiles[i];
                    String name = ftpFile.getName();
                    files.add(name);
                }
            }
            return files;
        } catch (IOException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        }
    }

    public String getHost() {
        return client.getPassiveHost();// return host;
    }

    public String getPath() {
        // FIXME
        return null;
    }

    public boolean isFileExist(String path, String fileName) throws  FtpException {
        FTPFile[] listFiles;
        try {
            listFiles = client.listFiles(path);
        if (listFiles != null && listFiles.length > 0) {
            for (int i = 0; i < listFiles.length; i++) {
                FTPFile ftpFile = listFiles[i];
                String name = ftpFile.getName();
                boolean equals = name.equals(fileName);
                if (equals)
                    return true;
            }
        }
        } catch (IOException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        }
        return false;
    }

    public boolean isFileExist(String fileName) throws  FtpException {
        FTPFile[] listFiles;
        try {
            listFiles = client.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (int i = 0; i < listFiles.length; i++) {
                FTPFile ftpFile = listFiles[i];
                String name = ftpFile.getName();
                boolean equals = name.equals(fileName);
                if (equals)
                    return true;
            }
        }
        } catch (IOException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        }
        return false;
    }

    public void upload(String localFile, String newFileName) throws  FtpException {
        InputStream local;
        try {
            local = new FileInputStream(localFile);
            client.storeFile(newFileName, local);
        } catch (FileNotFoundException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        } catch (IOException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        }

    }

    public void upload(String localFile) throws  FtpException {
        InputStream local;
        try {
            local = new FileInputStream(localFile);
            client.storeUniqueFile(local);
        } catch (FileNotFoundException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        } catch (IOException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        }

    }

    public List<FTPFile> getFileInfos(String path) throws FtpException {
        FTPFile[] listFiles;
        try {
            listFiles = client.listFiles(path);
            List<FTPFile> files = new ArrayList<FTPFile>();
            if (listFiles != null && listFiles.length > 0) {
                for (int i = 0; i < listFiles.length; i++) {
                    FTPFile ftpFile = listFiles[i];
                   
                    files.add(ftpFile);
                }
            }
            return files;
        } catch (IOException e) {
            FtpException ftpException = new FtpException();
            ftpException.initCause(e);
            throw ftpException;
        }
    }

}
